package com.farmproduct.service;

import com.farmproduct.dao.ProductMapper;
import com.farmproduct.pojo.Product;
import com.farmproduct.pojo.ProductExample;
import com.farmproduct.tools.QRCodeUtils;
import com.farmproduct.vo.ConditionSearch;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductExample productExample;
    private String imageaddress = "http://www.terrow.top:9906/";

    public String getImageaddress() {
        return imageaddress;
    }

    public void setImageaddress(String imageaddress) {
        this.imageaddress = imageaddress;
    }

    /**
     * 添加产品
     * */
    public boolean addProduct(Product product){
        productExample.clear();
        if (productMapper.insert(product)==1 && this.makeQRCode(product.getPid())){
            return true;
        }
        return false;
    }

    /**
     * 删除产品
     * */
    public boolean delProduct(Product product){
        productExample.clear();
        if (productMapper.deleteByPrimaryKey(product.getPid())==1 && this.delQRCode(product.getPid())){
            return true;
        }
        return false;
    }
    /**
     * 根据产品集合删除产品
     * @return 是否批量删除成功
     * */
    public boolean delProductList(List<Product> products){
        if (products.size()>0){
            for (Product product : products) {
                if(productMapper.deleteByPrimaryKey(product.getPid())!=1 || !this.delQRCode(product.getPid())){
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    public long getProductConuntByUid(String uid){
        productExample.clear();
        productExample.createCriteria().andUidEqualTo(uid);
        return productMapper.countByExample(productExample);
    }



    /**
     * 更新产品
     * */
    public boolean updateProduct(Product product){
        productExample.clear();
        if (productMapper.updateByPrimaryKey(product)==1){
            return true;
        }
        return false;
    }

    /**
     * 根据用户获取商品
     * */
    public List<Product> getProductByUidOrder(String uid){
        List<Product> productByUidOrder = productMapper.getProductByUidOrder(uid);
        if (productByUidOrder!=null){
            return productByUidOrder;
        }
        return null;
    }

    public List<Product> getProductByUid(String uid){
        productExample.clear();
        productExample.createCriteria().andUidEqualTo(uid);
        List<Product> products = productMapper.selectByExample(productExample);
        if (products!=null){
            return products;
        }
        return null;
    }
    /**
     * 获取商品
     * */
    public Product getProductByPid(String pid){
        productExample.clear();
        productExample.createCriteria().andPidEqualTo(pid);
        List<Product> products = productMapper.selectByExample(productExample);
        if (products!=null && products.size()>0){
            Product product = products.get(0);
            return product;
        }
        return null;
    }

    /**
     * 根据条件获取产品对象集合
     * */
    public List<Product> getProductsByCondition(ConditionSearch conditionSearch){
        productExample.clear();
        ProductExample.Criteria criteria = productExample.createCriteria();
        criteria.andUidLike(conditionSearch.getUid());
        if (conditionSearch.getPid()!=""){
            criteria.andPidEqualTo(conditionSearch.getPid());
        }
        if (conditionSearch.getPtype()!=""){
            criteria.andPtypeLike("%"+conditionSearch.getPtype()+"%");
        }
        if (conditionSearch.getDestination()!=""){
            criteria.andDestinationLike("%"+conditionSearch.getDestination()+"%");
        }
        List<Product> products = productMapper.selectByExample(productExample);
        if (products!=null){
            return products;
        }
        return null;
    }

    public boolean makeQRCode(String pid){
        String url = imageaddress+"/product.html?pid="+pid;
        try {
            QRCodeUtils qrCodeUtils = new QRCodeUtils();
            qrCodeUtils.myEncode(url,pid);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean delQRCode(String pid){
        try {
            QRCodeUtils.delQRCode(pid);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

}
